package com.glsc.ngateway.platform.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.GwContact;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.api.platform.dto.system.GwContactDto;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.service.system.GwContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/system/gwContract")
@Api(value = "联系人", tags = {"联系人"})
public class GwContractController {

    private static final Logger logger = LoggerFactory.getLogger(GwContractController.class);
    @Autowired
    private GwContractService gwContractService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserCache userCache;
    @Autowired
    private UserService userService;

    /*
     * 新增或修改
     */
    @RequestMapping(method = RequestMethod.POST, value = "addOrUpdate")
    @ApiOperation(value = "新增或修改", notes = "新增或修改")
    public PlatformResponse<Void> addOrUpdate(@RequestBody GwContact gwContract) {
        gwContractService.addOrUpdate(gwContract);
        return PlatformResponse.successMsg("创建或修改成功");
    }

    /**
     * 没有删除接口，只有下线
     */
    @RequestMapping(method = RequestMethod.POST, value = "delete")
    @ApiOperation(value = "删除", notes = "删除")
    public PlatformResponse<Void> delete(@RequestBody String param) throws Exception {
        if (StringUtils.isEmpty(param)) {
            return PlatformResponse.failedMsg("delete接口，参数为空。");
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(param);
        Integer id = jsonObject.getInteger("id");
        if (id != null) {
            gwContractService.delete(id);
        }
        return PlatformResponse.successMsg("删除成功");
    }

    /**
     * 查询
     * @param params
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "search")
    @ApiOperation(value = "查询", notes = "查询")
    public PlatformResponse<Page<GwContact>> search(@RequestBody GwContactDto params) {
        Page<GwContact> result = gwContractService.search(params);
        return PlatformResponse.successData(result);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "queryById")
    @ApiOperation(value = "查询", notes = "查询")
    public PlatformResponse<GwContact> queryById(@RequestParam("id") Integer id) {
        GwContact gwContact = gwContractService.findById(id);
        if(null!=gwContact){
            return PlatformResponse.successData(gwContact);
        }
        return PlatformResponse.failedMsg("未查询到数据！");
    }

    /**
     * 根据idList查询
     * @param idList
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "queryByIdList")
    @ApiOperation(value = "查询", notes = "查询")
    public PlatformResponse<List<GwContact>> queryByIdList(@RequestBody List<Integer> idList) {
        List<GwContact> gwContactList = gwContractService.findByIdList(idList);
        return PlatformResponse.successData(gwContactList);
    }
}